const Service = require("egg").Service;

const selectUserColumns = [
  "user_id",
  "username",
  "realname",
  "avatar",
  "phone",
  "email",
  "sex",
  "createtime",
  "updatetime",
];

class UserService extends Service {
  // 检查用户名是否存在
  async checkUser(params) {
    const { app } = this;
    const result = await app.mysql.select("admin_user", {
      where: {
        username: params.username,
      },
    });
    return !!result[0];
  }
  // 注册用户
  async registerUser(params) {
    const { app, ctx } = this;
    const res = await app.mysql.insert("admin_user", {
      user_id: ctx.uuid(),
      username: params.username,
      password: params.password,
      createtime: ctx.cTime(),
      updatetime: ctx.cTime(),
      ...params,
    });
    return res;
  }
  // 登陆
  async login(params) {
    const { app } = this;
    const res = await app.mysql.select("admin_user", {
      where: {
        username: params.username,
      },
    });
    if (res[0]) {
      if (res[0].password === params.password) {
        return {
          msg: "登陆成功",
          data: res[0],
        };
      } else {
        return {
          msg: "密码错误",
          data: null,
        };
      }
    } else {
      return {
        msg: "用户不存在",
        data: null,
      };
    }
  }
  // 获取用户信息
  async getUserInfo() {
    const { app, ctx } = this;
    return await app.mysql.select("admin_user", {
      columns: [
        "user_id",
        "username",
        "realname",
        "phone",
        "email",
        "sex",
        "avatar",
        "createtime",
        "updatetime",
      ],
      where: {
        user_id: ctx.state.user.user_id,
      },
    });
  }
  // 获取用户列表
  async getUserList(params) {
    const { ctx, app } = this;
    // 查询数据总数
    const total = await app.mysql.select("admin_user");

    let queryStr;

    const condition = ctx.condition(params);
    if (condition.has) {
      queryStr = `select ${selectUserColumns.join(
        ","
      )} from admin_user where ${condition.str} order by createtime DESC limit ${
        params.pagesize
      } offset ${params.pagesize * params.pagenum - params.pagesize}`;
    } else {
      queryStr = `select ${selectUserColumns.join(
        ","
      )} from admin_user order by createtime DESC limit ${
        params.pagesize
      } offset ${params.pagesize * params.pagenum - params.pagesize}`;
    }
    const data = await app.mysql.query(queryStr);
    return { data, total: total.length };
  }
  // 编辑用户
  async editUser(params) {
    const { ctx, app } = this;
    return await app.mysql.update(
      "admin_user",
      {
        username: params.username,
        phone: params.phone,
        realname: params.realname,
        email: params.email,
        sex: params.sex,
        updatetime: ctx.cTime(),
      },
      {
        where: {
          user_id: params.user_id,
        },
      }
    );
  }
  // 删除用户
  async deleteUser(params) {
    const { app } = this;
    return await app.mysql.delete("admin_user", {
      user_id: params.user_id,
    });
  }
  // 修改用户头像
  async changeAvatar(avatar) {
    const { ctx, app } = this;
    return await app.mysql.update(
      "admin_user",
      {
        avatar,
        updatetime: ctx.cTime(),
      },
      {
        where: {
          user_id: ctx.state.user.user_id,
        },
      }
    );
  }
}

module.exports = UserService;
